package com.proxy.cglib;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 每一行代码，都是改变世界的力量
 *
 * @author 郭帅
 * @date 2022/11/17 14:26
 */
public class MyInterceptor implements MethodInterceptor {

    private Object target;

    public MyInterceptor(Object target) {
        this.target = target;
    }


    /**
     * @param o           代理对象
     * @param method      被代理对象的方法
     * @param objects     方法入参
     * @param methodProxy 代理方法
     * @throws Throwable
     */
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("cglib before");
        // 调用代理类FastClass对象
//        Object result = methodProxy.invokeSuper(o, objects);
        Object result = methodProxy.invoke(target, objects);
        System.out.println("cglib after");
        return result;
    }


}
